﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using Mes.Pendant.Common.DtoMapping;
using Mes.Pendant.MaterialManagements;
using Mes.Pendant.MaterialManagementService.Dto;
using Mes.Pendant.MaterialManagementService.Services;
using Mes.Pendant.MaterialTypes;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Mes.Pendant.MaterialManagementServicess
{
	public class MaterialManagementServices : ApplicationService, IMaterialManagementServices
    {
        private readonly IRepository<MaterialManagement, int> repository;
        private readonly IRepository<MaterialType, int> repositoryType;
        public MaterialManagementServices(IRepository<MaterialManagement, int> repository, IRepository<MaterialType, int> repositoryType)
        {
            this.repository = repository;
            this.repositoryType = repositoryType;
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="materialCode"></param>
        /// <param name="materialName"></param>
        /// <param name="index"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public async Task<PagedResult> GetSearchMaterialManagements(string? ids, string? materialCode, string? materialName, int index = 1, int limit = 10)
        {
            var result = await repository.GetQueryableAsync();
            var result2 = await repositoryType.GetQueryableAsync();

            var settings = from x in result
                           join y in result2
                           on x.MaterialType equals y.Id
                           select new SearchMaterialManagement
                           {
                               Ida = x.Id,
                               Idb = y.Id,
                               MaterialName = x.MaterialName,
                               MaterialCode = x.MaterialCode,
                               SpecificationsModels = x.SpecificationsModels,
                               Unit = x.Unit,
                               Products = x.Products,
                               MaterialType = x.MaterialType,
                               State = x.State,
                               SafetyStock = x.SafetyStock,
                               MaterialTypeName = y.MaterialTypeName,
                           };
            if (!string.IsNullOrEmpty(ids))
            {
                string str = ids.Substring(0, ids.Length - 1);
                int[] aaa = Array.ConvertAll(str.Split(','), int.Parse);
                settings = settings.Where(x => aaa.Contains(x.Ida));
            }
            settings = settings.WhereIf(!string.IsNullOrEmpty(materialCode), list => list.MaterialCode.Contains(materialCode));
            settings = settings.WhereIf(!string.IsNullOrEmpty(materialName), list => list.MaterialName.Contains(materialName));
            PagedResult pagedResult = new PagedResult();
            pagedResult.CurrentPage = index;
            pagedResult.PageSize = limit;
            pagedResult.RowCount = settings.Count();
            pagedResult.PageCount = (int)Math.Ceiling((pagedResult.RowCount) / (double)limit);
            pagedResult.Queryable = settings.OrderBy(x => x.Ida).Skip((index - 1) * limit).Take(limit).AsQueryable();
            pagedResult.Queryable = pagedResult.Queryable.MapToList<SearchMaterialManagement>().AsQueryable();
            return pagedResult;
        }

        /// <summary>
        /// 添加物料产品
        /// </summary>
        /// <param name="materialmangement"></param>
        /// <returns></returns>
        public async Task<int> AddMaterialmanges(InsertMaterialMangementdto materialmangement)
        {
            var entity = ObjectMapper.Map<InsertMaterialMangementdto, MaterialManagement>(materialmangement);
            await repository.InsertAsync(entity);
            return 1;
        }

        /// <summary>
        /// 删除物料产品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteMaterialmanges(int id)
        {
            await repository.DeleteAsync(id);
            return 1;
        }
    }
}
